<?php

class PermissionController extends Controller
{
	public $defaultAction = 'index';
	public $layout='menu';

	public function actionIndex($id = null)
	{
			Yii::app()->params['pvt'] = 'permissionManage';
			Yii::app()->params['top'] = 'accessTop';
			$arrRoleList = array();
			$arrGroupList = array();
			$found = null;
			$arrView = array();
			$arrUpdate = array();
			$role = null;
			
			if(!isset($_POST['role']))
				$_POST['role'] = $id;
			
			if(isset($_POST['role']))
			{
				$role = $_POST['role'];
		
			}
		
		if($_POST )
		{	
			//print_r($_POST['role_id']);exit;
					if(isset($_POST['role']))
					{
						$found = Permission::model()->findByAttributes(array('role_id'=>$role));
					if($found)
						{
								//$role = $_Post['role'];
								$found = Permission::model()->findAllByAttributes(array('role_id'=>$role));
										//$arrRoleList = Role::getList('ROLE-ID');
										//$arrGroupList = Group::getList('GROUP-ID');
								  
									foreach ($found as $value) {
										
										$arrView[] = $value->view_access;
										
										$arrUpdate[] = $value->update_access;
									}
						}
						else
						{
							$found = Permission::model()->findAll();
									$arrRoleList = Role::getList('ROLE-ID');
									$arrGroupList = Group::getList('GROUP-ID');
						}
						
					}
			
			if(isset($_POST['role_id']) && $_POST['role_id']!=null)
			{
				
			$group = array();
			$arr=$_POST;
			//$store = array();
			$role=$_POST['role_id'];
			unset($arr['yt0']);
			unset($arr['role_id']);
			
			$command = Yii::app()->db->createCommand();
			
			$command->update(Yii::app()->params['tbl_prefix'].'permission',
			array(
    						'view_access'=>'no',
							'update_access'=>'no',
			), 'role_id=:role_id', array(':role_id'=>$role));
			
			$sql=null;
			//var_dump($arr);
			//echo $role;
			//echo '<pre>';print_r($arr);
			if(!empty($arr['view']))
			$arrViewNew = $arr['view'];
			if(!empty($arr['update']))
			$arrUpdateNew = $arr['update'];
			
			//echo '<pre>';print_r($arrViewNew);
			//echo '<pre>';print_r($arrUpdateNew);exit;
			if (!empty($arrViewNew)){
			foreach ($arrViewNew as $key => $value) {
				$group_id = $key ;
				$found = Permission::model()->findByAttributes(array('role_id'=>$role, 'group_id'=>$group_id));
				if (empty($found))
				{
					$command->insert(Yii::app()->params['tbl_prefix'].'permission', array(
    							'role_id'=>$role,
								'group_id'=>$group_id,
								'view_access'=>$value,
					));
				}
				else
				{
					$command->update(Yii::app()->params['tbl_prefix'].'permission',
								array(
											'role_id'=>$role,
											'group_id'=>$group_id,
				    						'view_access'=>$value,
									),'group_id=:group_id and role_id=:role_id',array(':role_id'=>$role,':group_id'=>$group_id));
				}
			}
			}
			//echo '<pre>';print_r($arrUpdateNew);exit;
			if (!empty($arrUpdateNew)){
			foreach ($arrUpdateNew as $key => $value) {
				$group_id = $key ;
				$found = Permission::model()->findByAttributes(array('role_id'=>$role, 'group_id'=>$group_id));
				if (empty($found))
				{
					$command->insert(Yii::app()->params['tbl_prefix'].'permission', array(
    							'role_id'=>$role,
								'group_id'=>$group_id,
								'update_access'=>$value,
					));
				}
				else
				{
					$command->update(Yii::app()->params['tbl_prefix'].'permission',
								array(
											'role_id'=>$role,
											'group_id'=>$group_id,
				    						'update_access'=>$value,
									),'group_id=:group_id and role_id=:role_id',array(':role_id'=>$role,':group_id'=>$group_id));
				}
			}
			}
			
			/*foreach ($arr as $key => $value) {
				$group[$key]=$value;				
				$keytemp=$key[0];
				
				$arrKey = explode("_", $key);
				//var_dump($arrKey);
				
				//$store = $key;
				
				$view = $group[$keytemp."_view"];
				if($view == '')
				$view = 'no';
				
				$update = $group[$keytemp."_update"];
				if($update == '')
				$update = 'no';
				
				$found = Permission::model()->findByAttributes(array('role_id'=>$role, 'group_id'=>$keytemp));
				if(!$found)
				{
					//exit("insert");
					$command->insert('pdn_permission', array(
    							'role_id'=>$role,
								'group_id'=>$keytemp,
								'view_access'=>$view,
								'update_access'=>$update,
					));
				}
				else
				{
						$command->update('pdn_permission',
								array(
											'role_id'=>$role,
											'group_id'=>$keytemp,
				    						'view_access'=>$view,
											'update_access'=>$update,
									),'group_id=:group_id and role_id=:role_id',array(':role_id'=>$role));
					
				}	*/
			}
			
					
		}
			
		   $found = Permission::model()->findAll();
			$arrRoleList = Role::getList('ROLE-ID');
			$arrGroupList = Group::getList('GROUP-ID');
	//echo $role;		
	if($role)
	{
		//$role = $_Post['role'];
		$found = Permission::model()->findAll('role_id = :role_id ',array(':role_id'=>$role));
		//$arrRoleList = Role::getList('ROLE-ID');
		//$arrGroupList = Group::getList('GROUP-ID');
		// var_dump($arrGroupList);
			foreach ($found as $value) {
				//$group_id = $value->group_id;
				//echo '<pre>';print_r($group_id);
				$arrView[$value->group_id] = $value->view_access;
				$arrUpdate[$value->group_id] = $value->update_access;
			}
		}
	else
	{
		$found = Permission::model()->findAll();
				$arrRoleList = Role::getList('ROLE-ID');
				$arrGroupList = Group::getList('GROUP-ID');
	}
	//var_dump($arrView);
	//var_dump($arrUpdate);
	//echo '<pre>';print_r($arrView);
	//echo '<pre>';print_r($arrUpdate);exit;
		
		$this->render('index',array('role'=>($role)? $role : null ,'arrRoleList'=>$arrRoleList, 'arrGroupList'=>$arrGroupList, 'arrView'=>$arrView, 'arrUpdate'=>$arrUpdate,'id'=>$id));
	}





	/*public function actionCreate()
	{

		if($_POST)
		{
				
			$group = array();
			$arr=$_POST;
			$store = array();
			//echo '<pre>'; print_r($_POST); exit;
			$role=$arr['role'];

			unset($arr[yt0]);
			unset($arr[role]);
			//echo '<pre>'; print_r($arr);

			$command = Yii::app()->db->createCommand();

			$command->update('pdn_permission',
			array(
    						'view_access'=>'no',
							'update_access'=>'no',
			), 'role_id=:role_id', array(':role_id'=>$role));
			//exit;
			$sql=null;
			foreach ($arr as $key => $value) {
				$group[$key]=$value;
				//print_r("$group");exit;
					
				$keytemp=$key[0];
					
				$store = $key;
					
				$view = $group[$keytemp."_view"];
					
				if($view == '')
				$view = 'no';

				$update = $group[$keytemp."_update"];
				if($update == '')
				$update = 'no';

				$found = Permission::model()->findByAttributes(array('role_id'=>$role));
				if(!$found)
				{

					//exit('hrtere')	;
					$command->insert('pdn_permission', array(
    							'role_id'=>$role,
								'group_id'=>$keytemp,
								'view_access'=>$view,
								'update_access'=>$update,
					));
				}
				else
				{

					$command->update('pdn_permission',
					array(
    						'view_access'=>$view,
							'update_access'=>$update,
					), 'role_id=:role_id and group_id=:group_id', array(':role_id'=>$role, ':group_id'=>$keytemp));
					//exit("update");
				}
					
				// $command=$connection->createCommand($sql);
				// $dataReader=$command->query();
				//$sql=null;
			}
		}
		$this->redirect(array('index'));
	}*/
}
